//
//  QDAboutHeaderView.swift
//  QDMetroClient
//
//  Created by YueWen on 2019/2/28.
//  Copyright © 2019 YueWen. All rights reserved.
//

import UIKit

protocol AppVersion {
    
    /// app的名称
    var appName: String { get }
}

extension AppVersion {
    
    var appName: String {
        
        guard let bundle = Bundle.main.infoDictionary else {
            return ""
        }
        
        return bundle["CFBundleDisplayName"] as? String ?? "青岛地铁"
        
    }
}

/// 关于的表头
final class QDAboutHeaderView: UIView,AppVersion {

    /// app的图标
    let appIcon = UIImageView()
    /// app的标题
    let appNameLabel = UILabel()
    /// app的版本
    let appVersionLabel = RITLExternLabel()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        appIcon.contentMode = .scaleAspectFit
        appIcon.image = "AppIcon".namedImage
        
        appNameLabel.text = "\(appName)APP"
        appNameLabel.font = Font.medium.font(size: 20)
        appNameLabel.textColor = QDColors.black
        
        appVersionLabel.text = "V\(NSDictionary.ritl_version ?? "")"
        appVersionLabel.textAlignment = .center
        appVersionLabel.textColor = #colorLiteral(red: 0.7607843137, green: 0.768627451, blue: 0.7647058824, alpha: 1)
        appVersionLabel.extensionSpace = 8
        appVersionLabel.font = Font.regular.font(size: 12)
        appVersionLabel.layer.borderWidth = 0.5
        appVersionLabel.layer.borderColor = #colorLiteral(red: 0.7607843137, green: 0.768627451, blue: 0.7647058824, alpha: 1).cgColor
        appVersionLabel.layer.cornerRadius = 8
        appVersionLabel.clipsToBounds = true
        
        addSubview(appIcon)
        addSubview(appNameLabel)
        addSubview(appVersionLabel)
        
        appIcon.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.height.width.equalTo(88)
            make.top.equalToSuperview().offset(40)
        }
        
        appNameLabel.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.height.equalTo(28)
            make.top.equalTo(appIcon.snp.bottom).offset(8)
        }
        
        appVersionLabel.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.height.equalTo(16)
            make.top.equalTo(appNameLabel.snp.bottom).offset(4)
        }
    }
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
